<template>
    <div class="page-box">
        <Card>
            <Button size="small" type="primary" @click="returnDaily">返回</Button>
            <el-table
                :data="data"
                height="calc(100vh - 195px)"
                border
                class="margin-top-5"
                style="width: 100%"
                size="mini"
                highlight-current-row
                :header-cell-style="headerCellStyle"
                oncontextmenu="return false;"
                ref="multipleTable"
                sortable="custom"
                empty-text="暂无数据"
                v-loading="dataLoading"
                show-summary
                :summary-method="getSummaries"
                @sort-change="onTableSort">
                <el-table-column
                    v-for="item in columns"
                    :key="item.prop"
                    :prop="item.prop"
                    :label="item.label"
                    :width="item.width"
                    :align="item.align"
                    :sortable="item.sortable"
                    :show-overflow-tooltip="true"
                    :fixed="item.fixed"
                >
                    <template slot-scope="scope">
                        <template v-if="item.type !== 'template'">
                            {{ $table.tableRowFormat(scope.row, item) }}
                        </template>
                    </template>
                </el-table-column>
                <el-table-column v-if="$route.query.key === 'in' || $route.query.key === 'out' || $route.query.key === 'separate'"  label="操作" header-align="center" align="center" width="100">
                    <template slot-scope="scope">
                        <el-button style="color: #4f68e8" size="small" type="text"
                                   @click="clickNode($route.query.key, scope.row)">详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Row class="margin-top-10">
                <Page :total="total" :current="listQuery.pageNum" :page-size="listQuery.pageSize"
                      :page-size-opts="listQuery.pageOptionsList"
                      size="small" @on-change="pageChange" @on-page-size-change="pageSizeChange" show-elevator
                      show-sizer></Page>
            </Row>
        </Card>
    </div>
</template>

<script>
import {getConfig} from '../json/config'
import {
    inStorageDailyData,
    outStorageDailyData,
    separateDailyData,
    stockTransferInDailyData,
    stockTransferOutDailyData,
    inStorageDailyDataSum,
    outStorageDailyDataSum,
    separateDailyDataSum,
    stockTransferInDailyDataSum,
    stockTransferOutDailyDataSum
} from '@/api/statisticalModule.js';

export default {
    name: 'ContainerNoInfo',
    data() {
        return {
            totalData: {},
            sumData: {},
            data: [],
            columns: [],
            dataLoading: false,
            total: 0,
            spanArr: [],
            pos: 0,
            listQuery: {
                pageNum: this.$store.state.sysSettings.pageNum,
                pageSize: this.$store.state.sysSettings.pageSize
            },
        };
    },
    computed: {
        headerCellStyle() {
            return {
                fontSize: `${12}px`,
                fontWeight: 'bold',
                color: '#8c8c90',
                backgroundColor: 'rgb(239, 243, 247)',
            };
        }
    },
    methods: {
        returnDaily() {
            this.$router.back()
        },
        clickNode(key, row) {
            this.$router.push({
                path: 'containerdetails',
                query: {
                    key,
                    detailId: row.detailId,
                    wmsUrl: this.$route.query.wmsUrl
                }
            });
        },
        initGridData() {
            if (this.$route.query) {
                this.spanArr = [];
                this.pos = 0;
                // 接收传参
                let item = this.$route.query;
                this.listQuery.time = item.time;
                this.listQuery.ownerCode = item.ownerCode;
                this.dataLoading = true;
                if (item.key == 'in') {
                    inStorageDailyData(this.listQuery, item.wmsUrl).then(({data}) => {
                        this.dataLoading = false;
                        if (data.returnCode === 200) {
                            this.data = data.returnData.records;
                            this.total = data.returnData.total;
                            this.$nextTick(() => {
                                this.$refs.multipleTable.doLayout();
                            });
                        } else {
                            this.$error(response.data.returnMsg, 3);
                        }
                    }).catch(err => {
                        console.log(err);
                        this.dataLoading = false;
                        this.$loginOut();
                    });
                    this.getSumInfo(item.key);
                } else if (item.key == 'out') {
                    outStorageDailyData(this.listQuery, item.wmsUrl).then(({data}) => {
                        this.dataLoading = false;
                        if (data.returnCode === 200) {
                            this.data = data.returnData.records;
                            this.total = data.returnData.total;
                            this.$nextTick(() => {
                                this.$refs.multipleTable.doLayout();
                            });
                        } else {
                            this.$error(data.returnMsg, 3);
                        }
                    }).catch(err => {
                        console.log(err);
                        this.dataLoading = false;
                        this.$loginOut();
                    });
                    this.getSumInfo(item.key);
                } else if (item.key == 'separate') {
                    separateDailyData(this.listQuery, item.wmsUrl).then(({data}) => {
                        this.dataLoading = false;
                        if (data.returnCode === 200) {
                            this.data = data.returnData.records;
                            this.total = data.returnData.total;
                            this.$nextTick(() => {
                                this.$refs.multipleTable.doLayout();
                            });
                        } else {
                            this.$error(data.returnMsg, 3);
                        }
                    }).catch(err => {
                        console.log(err);
                        this.dataLoading = false;
                        this.$loginOut();
                    });
                    this.getSumInfo(item.key);
                } else if (item.key == 'stockin') {
                    stockTransferInDailyData(this.listQuery, item.wmsUrl).then(({data}) => {
                        this.dataLoading = false;
                        if (data.returnCode === 200) {
                            this.data = data.returnData.records;
                            this.total = data.returnData.total;
                            this.$nextTick(() => {
                                this.$refs.multipleTable.doLayout();
                            });
                        } else {
                            this.$error(data.returnMsg, 3);
                        }
                    }).catch(err => {
                        console.log(err);
                        this.dataLoading = false;
                        this.$loginOut();
                    });
                    this.getSumInfo(item.key);
                } else if (item.key == 'stockout') {
                    stockTransferOutDailyData(this.listQuery, item.wmsUrl).then(({data}) => {
                        this.dataLoading = false;
                        if (data.returnCode === 200) {
                            this.data = data.returnData.records;
                            this.total = data.returnData.total;
                            this.$nextTick(() => {
                                this.$refs.multipleTable.doLayout();
                            });
                        } else {
                            this.$error(data.returnMsg, 3);
                        }
                    }).catch(err => {
                        console.log(err);
                        this.dataLoading = false;
                        this.$loginOut();
                    });
                    this.getSumInfo(item.key);
                }
            }
        },
        // getSpanArr(data) {
        //     let type = this.$route.query.type;
        //     for (let i = 0; i < data.length; i++) {
        //         let key = type === 'in' ? 'businessNum' : 'carNo';
        //         if (i === 0) {
        //             this.spanArr.push(1);
        //             this.pos = 0;
        //         } else {
        //             if (data[i][key] === data[i - 1][key]) {
        //                 this.spanArr[this.pos] += 1;
        //                 this.spanArr.push(0);
        //             } else {
        //                 this.spanArr.push(1);
        //                 this.pos = i;
        //             }
        //         }
        //     }
        //     let index = 0;
        //     for (let n in this.spanArr) {
        //         if (this.spanArr[n] > 0) {
        //             index += 1;
        //             this.$set(this.data[n], 'index', index);
        //         }
        //     }
        // },
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2 || columnIndex === 3 || columnIndex === 4 || columnIndex === 12) {
                const _row = this.spanArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                };
            }
        },
        getSumInfo(key) {
            this.listQuery.time = this.$route.query.time;
            this.listQuery.ownerCode = this.$route.query.ownerCode;
            if (key == 'in') {
                inStorageDailyDataSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            } else if (key == 'out') {
                outStorageDailyDataSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            } else if (key == 'separate') {
                separateDailyDataSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            } else if (key == 'stockin') {
                stockTransferInDailyDataSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            } else if (key == 'stockout') {
                stockTransferOutDailyDataSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            }
        }
        ,
        selectionChange(selection) {
            this.selectDataList = selection;
        }
        ,
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '合';
                    return;
                }
                if (this.$route.query) {
                    let type = this.$route.query.key;
                    if (type === 'in') {
                        switch (index) {
                            case 14:
                                sums[index] = this.totalData.finishQty + '件';
                                break;
                            case 15:
                                sums[index] = this.totalData.finishWeight + 'KG';
                                break;
                        }
                    } else if (type === 'out') {
                        switch (index) {
                            case 15:
                                sums[index] = this.totalData.finishQty + '件';
                                break;
                            case 16:
                                sums[index] = this.totalData.finishWeight + 'KG';
                                break;
                        }
                    } else if (type === 'separate') {
                        switch (index) {
                            case 14:
                                sums[index] = this.totalData.finishQty + '件';
                                break;
                            case 15:
                                sums[index] = this.totalData.finishWeight + 'KG';
                                break;
                            case 16:
                                sums[index] = this.totalData.finishDeliverQty + '件';
                                break;
                            case 17:
                                sums[index] = this.totalData.finishDeliverWeight + 'KG';
                                break;
                        }
                    } else if (type === 'stockin' || type === 'stockout') {
                        switch (index) {
                            case 16:
                                sums[index] = this.totalData.finishQty + '件';
                                break;
                            case 17:
                                sums[index] = this.totalData.finishWeight + 'KG';
                                break;
                        }
                    } 
                    // else if (type === 'stockout') {
                    //     switch (index) {
                    //         case 16:
                    //             sums[index] = this.totalData.finishQty + '件';
                    //             break;
                    //         case 17:
                    //             sums[index] = this.totalData.finishWeight + 'KG';
                    //             break;
                    //     }
                    // }
                }
            });
            return sums;
        }
        ,
        // 排序
        onTableSort(result) {
            if (result.order === 'descending') {
                let sortItem = {
                    sortKey: result.prop,
                    sortValue: 'desc'
                };
                this.listQuery.sortList = [sortItem];
            } else if (result.order === 'ascending') {
                let sortItem = {
                    sortKey: result.prop,
                    sortValue: 'asc'
                };
                this.listQuery.sortList = [sortItem];
            } else {
                this.listQuery.sortList = [];
            }
            this.initGridData();
        }
        ,
        // 改变页数,
        pageChange(pageNum) {
            this.listQuery.pageNum = pageNum;
            this.initGridData();
        }
        ,
        // 改变条数
        pageSizeChange(pageSize) {
            this.listQuery.pageSize = pageSize;
            this.initGridData();
        },
        setConfig() {
            if (this.$route.query) {
            let type = this.$route.query.key;
            switch (type) {
                case 'in':
                    this.columns = getConfig.returnData.inColumns;
                    break;
                case 'out':
                     this.columns = getConfig.returnData.outColumns;
                    break;
                case 'separate':
                     this.columns = getConfig.returnData.separateColumns;
                    break;
                case 'stockin':
                     this.columns = getConfig.returnData.stockColumns;
                    break;
                case 'stockout':
                     this.columns = getConfig.returnData.stockColumns;
                    break;
                default:
                    break;
            }
        }
        }
    },
    mounted() {
        this.setConfig();
        this.initGridData();
    }
};
</script>

<style scoped lang="less">
@import '../common.less';

</style>
